//
//  YIMDB.swift
//  YIM
//
//  Created by YMac on 15/02/2022.
//  Copyright © 2022 zchd. All rights reserved.
//

import ObjectBox
import Foundation

internal class WGthmeUfEWrRecLn: NSObject {
    private var fnhwDWlsZZruIdXB = [String: Any]()
    
    private func hlXzmAhgLjQDwjKv<T: PIglkiKtQEIYkfTE>() throws -> Box<T>? {
        if NbDnvbpESqnqDInl.HNMTEjFdjzjxYWYx == nil {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB: Please init First!")
            return nil
        }
        if NbDnvbpESqnqDInl.ZOjIzOztbVARDuqG == nil || !fZwNztHkPUvazdeo.kKZHQgqVvWvEzcze.CJpHDPXJovTfKuUn() {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB: Please login first!")
            return nil
        }
        let yGxCIvPKkYdnsEub = try FileManager.default
            .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            .appendingPathComponent("yim")
            .appendingPathComponent("v2")
            .appendingPathComponent(NbDnvbpESqnqDInl.HNMTEjFdjzjxYWYx!.appKey!)
            .appendingPathComponent(NbDnvbpESqnqDInl.ZOjIzOztbVARDuqG!)
            .appendingPathComponent(String(describing: T.self).components(separatedBy: ".").last!)
        let BgtQidaGhwdIFTNS = yGxCIvPKkYdnsEub.path
        if !fnhwDWlsZZruIdXB.keys.contains(BgtQidaGhwdIFTNS) {
            try! FileManager.default.createDirectory(at: yGxCIvPKkYdnsEub, withIntermediateDirectories: true)
            let ZaMdwsKhJeMtyEPC = try Store(directoryPath: BgtQidaGhwdIFTNS)
            fnhwDWlsZZruIdXB[BgtQidaGhwdIFTNS] = ZaMdwsKhJeMtyEPC.box(for: T.self)
        }
        return fnhwDWlsZZruIdXB[BgtQidaGhwdIFTNS] as? Box<T>
    }
    
    func hyridvHtGRONajzD<T: PIglkiKtQEIYkfTE>(_ BfcJEqFGqwnZycwD: T.Type, gcspUzKnQvCPVWDj: (() -> QueryCondition<T>)? = nil) -> Int? {
        do {
            let UkVDqjwSsCyLZXDR: Box<T>? = try hlXzmAhgLjQDwjKv()
            if UkVDqjwSsCyLZXDR == nil { return nil }
            let RhNOPYfGfjrwMIPu = gcspUzKnQvCPVWDj == nil
                ? UkVDqjwSsCyLZXDR?.query()
                : UkVDqjwSsCyLZXDR?.query(gcspUzKnQvCPVWDj!)
            let mCzTRLyzjYCyanKb = try RhNOPYfGfjrwMIPu?.build()
            let hyridvHtGRONajzD = try mCzTRLyzjYCyanKb?.count()
            return hyridvHtGRONajzD
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_c: \(error.localizedDescription)")
            return nil
        }
    }
    
    func RhNOPYfGfjrwMIPu<T: PIglkiKtQEIYkfTE, V>(_ BfcJEqFGqwnZycwD: T.Type, gcspUzKnQvCPVWDj: (() -> QueryCondition<T>)? = nil, XDbstgapvwDPECiX: Property<T, V, Void>? = nil, XnhSWuJgBTGuusap: Bool = true, kScuDEUEUKAVKCOG: Int = 0, KLywTOzhcQSiFAmi: Int = 0) -> [T]? {
        do {
            let UkVDqjwSsCyLZXDR: Box<T>? = try hlXzmAhgLjQDwjKv()
            if UkVDqjwSsCyLZXDR == nil { return nil }
            var RhNOPYfGfjrwMIPu = gcspUzKnQvCPVWDj == nil
                ? UkVDqjwSsCyLZXDR?.query()
                : UkVDqjwSsCyLZXDR?.query(gcspUzKnQvCPVWDj!)
            if XDbstgapvwDPECiX != nil {
                RhNOPYfGfjrwMIPu = XnhSWuJgBTGuusap
                    ? RhNOPYfGfjrwMIPu?.ordered(by: XDbstgapvwDPECiX!)
                    : RhNOPYfGfjrwMIPu?.ordered(by: XDbstgapvwDPECiX!, flags: [.descending])
            }
            let mCzTRLyzjYCyanKb = try RhNOPYfGfjrwMIPu?.build()
            let EyTIVrNCTVqmYwPu = try mCzTRLyzjYCyanKb?.find(offset: kScuDEUEUKAVKCOG, limit: KLywTOzhcQSiFAmi)
            return EyTIVrNCTVqmYwPu
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_q: \(error.localizedDescription)")
            return nil
        }
    }
    
    func KamiWNsKhqtzmVUX<T: PIglkiKtQEIYkfTE>(_ BfcJEqFGqwnZycwD: T.Type, EeqbYNIBaZfkqYaF: T? = nil, xsKlHooJLsoavQGB: [T]? = nil, _: QueryCondition<T>? = nil) -> Bool {
        do {
            if (EeqbYNIBaZfkqYaF == nil && xsKlHooJLsoavQGB == nil) {
                NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_i: No Data")
                return false
            }
            let UkVDqjwSsCyLZXDR: Box<T>? = try hlXzmAhgLjQDwjKv()
            if UkVDqjwSsCyLZXDR == nil { return false }
            if EeqbYNIBaZfkqYaF != nil {
                try UkVDqjwSsCyLZXDR!.put(EeqbYNIBaZfkqYaF!)
            } else {
                try UkVDqjwSsCyLZXDR!.put(xsKlHooJLsoavQGB!)
            }
            return true
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_i: \(error.localizedDescription)")
            return false
        }
    }
    
    func ZOviHOAzCiIvfLxT<T: PIglkiKtQEIYkfTE, V>(_ BfcJEqFGqwnZycwD: T.Type, gcspUzKnQvCPVWDj: (() -> QueryCondition<T>)? = nil, tuATquJzrMWXRcKf: (([T]?) -> [T]?), mmtvsbscFeDeMSIO: ((_ EevOPNXlgWpEDUXw: T, _ iOWRlrkXHKdAMgbh: T) -> ()), XDbstgapvwDPECiX: Property<T, V, Void>) -> Bool {
        do {
            let UkVDqjwSsCyLZXDR: Box<T>? = try hlXzmAhgLjQDwjKv()
            if UkVDqjwSsCyLZXDR == nil { return false }
            let EyTIVrNCTVqmYwPu = RhNOPYfGfjrwMIPu(BfcJEqFGqwnZycwD, gcspUzKnQvCPVWDj: gcspUzKnQvCPVWDj, XDbstgapvwDPECiX: XDbstgapvwDPECiX)
            if EyTIVrNCTVqmYwPu == nil { return false }
            var i = 0
            tuATquJzrMWXRcKf(EyTIVrNCTVqmYwPu)?.forEach {
                mmtvsbscFeDeMSIO(EyTIVrNCTVqmYwPu![i], $0)
                i += 1
                let _ = try? UkVDqjwSsCyLZXDR!.put($0)
            }
            return true
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_u: \(error.localizedDescription)")
            return false
        }
    }
    
    func CZJecwYjjXBOhITh<T: PIglkiKtQEIYkfTE, V>(_ BfcJEqFGqwnZycwD: T.Type, gcspUzKnQvCPVWDj: (() -> QueryCondition<T>)? = nil, XDbstgapvwDPECiX: Property<T, V, Void>? = nil, XnhSWuJgBTGuusap: Bool = true, kScuDEUEUKAVKCOG: Int = 0, KLywTOzhcQSiFAmi: Int = 0) -> Bool {
        do {
            let UkVDqjwSsCyLZXDR: Box<T>? = try hlXzmAhgLjQDwjKv()
            if UkVDqjwSsCyLZXDR == nil { return false }
            let EyTIVrNCTVqmYwPu = RhNOPYfGfjrwMIPu(BfcJEqFGqwnZycwD, gcspUzKnQvCPVWDj: gcspUzKnQvCPVWDj, XDbstgapvwDPECiX: XDbstgapvwDPECiX, XnhSWuJgBTGuusap: XnhSWuJgBTGuusap, kScuDEUEUKAVKCOG: kScuDEUEUKAVKCOG, KLywTOzhcQSiFAmi: KLywTOzhcQSiFAmi)
            if EyTIVrNCTVqmYwPu == nil { return true }
            try UkVDqjwSsCyLZXDR?.remove(EyTIVrNCTVqmYwPu!)
            return true
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_d: \(error.localizedDescription)")
            return false
        }
    }
    
    func kglygDIoTqCxJKZd<T: PIglkiKtQEIYkfTE, V>(_ BfcJEqFGqwnZycwD: T.Type, gcspUzKnQvCPVWDj: (() -> QueryCondition<T>)? = nil, tuATquJzrMWXRcKf: ((T?) -> T), mmtvsbscFeDeMSIO: ((_ EevOPNXlgWpEDUXw: T, _ iOWRlrkXHKdAMgbh: T) -> ()), XDbstgapvwDPECiX: Property<T, V, Void>) -> Bool {
        do {
            let EyTIVrNCTVqmYwPu = RhNOPYfGfjrwMIPu(BfcJEqFGqwnZycwD, gcspUzKnQvCPVWDj: gcspUzKnQvCPVWDj, XDbstgapvwDPECiX: XDbstgapvwDPECiX)
            if EyTIVrNCTVqmYwPu == nil { return true }
            if EyTIVrNCTVqmYwPu!.isEmpty {
                let _ = KamiWNsKhqtzmVUX(BfcJEqFGqwnZycwD, EeqbYNIBaZfkqYaF: tuATquJzrMWXRcKf(nil))
            } else {
                let _ = ZOviHOAzCiIvfLxT(BfcJEqFGqwnZycwD, gcspUzKnQvCPVWDj: gcspUzKnQvCPVWDj, tuATquJzrMWXRcKf: { _ in
                    return [tuATquJzrMWXRcKf(EyTIVrNCTVqmYwPu!.first)]
                }, mmtvsbscFeDeMSIO: mmtvsbscFeDeMSIO, XDbstgapvwDPECiX: XDbstgapvwDPECiX)
            }
            return true
        } catch {
            NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.aKdyLQPwRZgdzpAx(IMuPBNSVQxljIWnu: "DB_uoi: \(error.localizedDescription)")
            return false
        }
    }
    
    func eFgNVOpBmPmJhaCd() {
//        fnhwDWlsZZruIdXB.removeAll()
    }
}

internal class PIglkiKtQEIYkfTE: NSObject, Codable {
}

// objectbox: entity
internal class _YIMTable_Message: PIglkiKtQEIYkfTE {
    var __: String = ""
    var t_id: Id = 0
    
    var id: String = ""
    var content: String?
    var from: String = ""
    var _fromInfo: String?
    
    // objectbox: transient
    var fromInfo: YIMInfo? {
        get {
            return _fromInfo == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.IYMCIgmwfHIBQjIX(_fromInfo, YIMInfo.self)!
        }
        set {
            _fromInfo = newValue == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.xtovshfHwqTsyirV(newValue!)
        }
    }
    
    var to: String = ""
    var _toInfo: String?
    
    // objectbox: transient
    var toInfo: YIMInfo? {
        get {
            return _toInfo == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.IYMCIgmwfHIBQjIX(_toInfo, YIMInfo.self)!
        }
        set {
            _toInfo = newValue == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.xtovshfHwqTsyirV(newValue!)
        }
    }
    var conversationType: String = ""
    var messageType: String = ""
    var messageState: String = ""
    var time: Int = 0
    var localExt: String?
    var isRevoke: String = "0"
    var _attachment: String?
    
    // objectbox: transient
    var attachment: YIMAttachment? {
        get {
            return _attachment == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.IYMCIgmwfHIBQjIX(_attachment, YIMAttachment.self)!
        }
        set {
            _attachment = newValue == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.xtovshfHwqTsyirV(newValue!)
        }
    }
    
    override init() {
        super.init()
    }
    
    init(GZRevkeIIdHLwXxe: YIMMessage) {
        super.init()
        self.id = GZRevkeIIdHLwXxe.id
        self.content = GZRevkeIIdHLwXxe.content
        self.from = GZRevkeIIdHLwXxe.from
        self.fromInfo = GZRevkeIIdHLwXxe.fromInfo
        self.to = GZRevkeIIdHLwXxe.to
        self.toInfo = GZRevkeIIdHLwXxe.toInfo
        self.conversationType = GZRevkeIIdHLwXxe.conversationType.rawValue
        self.messageType = GZRevkeIIdHLwXxe.messageType.rawValue
        self.messageState = GZRevkeIIdHLwXxe.messageState.rawValue
        self.time = GZRevkeIIdHLwXxe.time
        self.localExt = GZRevkeIIdHLwXxe.localExt == nil ? nil : NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.xtovshfHwqTsyirV(GZRevkeIIdHLwXxe.localExt!)
        self.isRevoke = GZRevkeIIdHLwXxe.isRevoke ? "1" : "0"
        self.attachment = GZRevkeIIdHLwXxe.attachment
    }
    
    func update(GZRevkeIIdHLwXxe: _YIMTable_Message) {
        id = GZRevkeIIdHLwXxe.id
        content = GZRevkeIIdHLwXxe.content
        from = GZRevkeIIdHLwXxe.from
        fromInfo = GZRevkeIIdHLwXxe.fromInfo
        to = GZRevkeIIdHLwXxe.to
        toInfo = GZRevkeIIdHLwXxe.toInfo
        conversationType = GZRevkeIIdHLwXxe.conversationType
        messageType = GZRevkeIIdHLwXxe.messageType
        messageState = GZRevkeIIdHLwXxe.messageState
        time = GZRevkeIIdHLwXxe.time
        localExt = GZRevkeIIdHLwXxe.localExt
        isRevoke = GZRevkeIIdHLwXxe.isRevoke
        attachment = GZRevkeIIdHLwXxe.attachment
    }
    
    private enum CodingKeys: String, CodingKey {
        case id
        case content
        case from
        case _fromInfo = "fromInfo"
        case to
        case _toInfo = "toInfo"
        case conversationType
        case messageType
        case messageState
        case time
        case localExt
        case isRevoke
        case _attachment = "attachment"
    }
    
    override func encode(to EMNYQVvqNJiIgaqk: any Encoder) throws {
        var SrUGOTAxXIcdNHHF = EMNYQVvqNJiIgaqk.container(keyedBy: CodingKeys.self)
        try SrUGOTAxXIcdNHHF.encode(id, forKey: .id)
        try SrUGOTAxXIcdNHHF.encode(content, forKey: .content)
        try SrUGOTAxXIcdNHHF.encode(from, forKey: .from)
        try SrUGOTAxXIcdNHHF.encode(fromInfo, forKey: ._fromInfo)
        try SrUGOTAxXIcdNHHF.encode(to, forKey: .to)
        try SrUGOTAxXIcdNHHF.encode(toInfo, forKey: ._toInfo)
        try SrUGOTAxXIcdNHHF.encode(conversationType, forKey: .conversationType)
        try SrUGOTAxXIcdNHHF.encode(messageType, forKey: .messageType)
        try SrUGOTAxXIcdNHHF.encode(messageState, forKey: .messageState)
        try SrUGOTAxXIcdNHHF.encode(time, forKey: .time)
        try SrUGOTAxXIcdNHHF.encode(localExt, forKey: .localExt)
        try SrUGOTAxXIcdNHHF.encode(isRevoke, forKey: .isRevoke)
        try SrUGOTAxXIcdNHHF.encode(attachment, forKey: ._attachment)
    }
    
    required public init(from sfUPMcOtmkluyPKK: any Decoder) throws {
        super.init()
        let SrUGOTAxXIcdNHHF = try sfUPMcOtmkluyPKK.container(keyedBy: CodingKeys.self)
        id = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .id)
        content = try SrUGOTAxXIcdNHHF.decodeIfPresent(String.self, forKey: .content)
        if let umLPpQHNmFlDeYyH = try SrUGOTAxXIcdNHHF.decodeIfPresent(String.self, forKey: .content) {
            content = umLPpQHNmFlDeYyH
        }
        from = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .from)
        if let YkJaaNUSklqAodtF = try SrUGOTAxXIcdNHHF.decodeIfPresent(YIMInfo.self, forKey: ._fromInfo) {
            fromInfo = YkJaaNUSklqAodtF
        }
        to = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .to)
        if let ddtFGqBbtUUqSrrq = try SrUGOTAxXIcdNHHF.decodeIfPresent(YIMInfo.self, forKey: ._toInfo) {
            toInfo = ddtFGqBbtUUqSrrq
        }
        conversationType = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .conversationType)
        messageType = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .messageType)
        messageState = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .messageState)
        time = try SrUGOTAxXIcdNHHF.decode(Int.self, forKey: .time)
        if let LOMPQKzpsgGrXaib = try SrUGOTAxXIcdNHHF.decodeIfPresent(String.self, forKey: .localExt) {
            localExt = LOMPQKzpsgGrXaib
        }
        isRevoke = try SrUGOTAxXIcdNHHF.decode(String.self, forKey: .isRevoke)
        if let JysBCFnGQzbcFFoA = try SrUGOTAxXIcdNHHF.decodeIfPresent(YIMAttachment.self, forKey: ._attachment) {
            attachment = JysBCFnGQzbcFFoA
        }
    }
}

// objectbox: entity
internal class _YIMTable_Conversation: PIglkiKtQEIYkfTE {
    var __: String?
    var t_id: Id = 0
    
    var _lastMessage: String = ""
    var unRead: Int = 0
    var isTop: Bool = false
    var isDisturb: Bool = false
    
    // objectbox: transient
    var lastMessage: _YIMTable_Message {
        get {
            return NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.IYMCIgmwfHIBQjIX(_lastMessage, _YIMTable_Message.self)!
        }
        set {
            _lastMessage = NbDnvbpESqnqDInl.zYOvKsmnWpOUpLXr.xtovshfHwqTsyirV(newValue)
        }
    }
    
    //子类型字段查询使用
    var _from: String = ""
    var _to: String = ""
    var _time: Int = 0
    var _conversationType: String = ""
    
    override init() {
        super.init()
    }
    
    init(xMeiNXsxexQoqySe: YIMConversation) {
        super.init()
        self.lastMessage = _YIMTable_Message(GZRevkeIIdHLwXxe: xMeiNXsxexQoqySe.lastMessage)
        self.unRead = xMeiNXsxexQoqySe.unRead
        self.isTop = xMeiNXsxexQoqySe.isTop
        self.isDisturb = xMeiNXsxexQoqySe.isDisturb
    }
    
    private enum CodingKeys: String, CodingKey {
        case _lastMessage = "lastMessage"
        case unRead
        case isTop
        case isDisturb
    }
    
    override func encode(to EMNYQVvqNJiIgaqk: any Encoder) throws {
        var SrUGOTAxXIcdNHHF = EMNYQVvqNJiIgaqk.container(keyedBy: CodingKeys.self)
        try SrUGOTAxXIcdNHHF.encode(lastMessage, forKey: ._lastMessage)
        try SrUGOTAxXIcdNHHF.encode(unRead, forKey: .unRead)
        try SrUGOTAxXIcdNHHF.encode(isTop, forKey: .isTop)
        try SrUGOTAxXIcdNHHF.encode(isDisturb, forKey: .isDisturb)
    }
    
    required public init(from sfUPMcOtmkluyPKK: any Decoder) throws {
        super.init()
        let SrUGOTAxXIcdNHHF = try sfUPMcOtmkluyPKK.container(keyedBy: CodingKeys.self)
        lastMessage = try SrUGOTAxXIcdNHHF.decode(_YIMTable_Message.self, forKey: ._lastMessage)
        unRead = try SrUGOTAxXIcdNHHF.decode(Int.self, forKey: .unRead)
        isTop = try SrUGOTAxXIcdNHHF.decode(Bool.self, forKey: .isTop)
        isDisturb = try SrUGOTAxXIcdNHHF.decode(Bool.self, forKey: .isDisturb)
    }
}
